<template>
	<div class="evaluate-operation">
		<div class="classIndex_h">
		    <a @click="goBack">综合素质评价</a>
		    <strong><i>></i>评价管理</strong>
		</div>

		<div class="evaluate-add"><el-button @click="evaluateAdd" type="primary" icon="el-icon-plus">新建评价</el-button></div>	
		<template v-if="studentList.length != 0">
		  <el-table
		    :data="studentList"
		    border
		    style="width: 100%">
		    <el-table-column
		      prop="termname"
		      label="学期"
		      align="center">
		    </el-table-column>
		    <el-table-column
		      prop="states"
		      label="状态"
		      width="100"
		      align="center">
		    </el-table-column>
		    <el-table-column
		      prop="time"
		      label="截止时间"
		      align="center">
		    </el-table-column>
		    <el-table-column
		      label="操作"
		      width="100"
		      align="center">
		      <template slot-scope="scope">
		      	<router-link  v-if="scope.row.states == '进行中'" :to="{path: '/class/quality/edit', query: {id: path, tid: scope.row.termid, eid: scope.row.id}}">编辑</router-link>
				<el-button  type="text" @click="removeList(scope.row)">删除</el-button>
		      </template>
		    </el-table-column>
		  </el-table>
		</template>

		<div class="dependent" v-if="placeholder">
			<i class="ipm image_nofound"></i>
			<p>
				还没发现任何相关的内容哦~
			</p>
		</div>

		<el-pagination class="quality-page" v-if="sunTotal > 10"
		      @size-change="sizeChange"
		      @current-change="currentChange"
		      :current-page="page"
		      :page-sizes="[10, 50, 100, 200, 300, 400]"
		      :page-size="size"
		      layout="total, sizes, prev, pager, next, jumper"
		      :total="sunTotal">
		    </el-pagination>

	</div>
</template>

<script>
	
	export default{
		data(){
			return {
				path: this.$route.query.id,
				studentList: [],
				page: 1,
				size: 10,
				sunTotal: 0,
				placeholder: false
			}
		},
		watch: {
			'$store.state.changData': {
				handler(val, newval){
					this.path = sessionStorage.getItem('classId')
					this.getEvaluate()
					
				},
				immediate: true
			}
		},
		methods: {
			changeDate(){
				var date = new Date();
				var year = date.getFullYear();
				var month = date.getMonth() + 1;
				var day = date.getDate();
				if (month < 10) {
				    month = "0" + month;
				}
				if (day < 10) {
				    day = "0" + day;
				}

				return `${year}-${month}-${day}`

			},
			getEvaluate(){
				let success =(res=>{
					// this.studentList = res.data
					this.studentList = []
					this.page = +res.data.page_index;
					this.sunTotal = res.data.data_sum;

					if (res.data.data.length != 0) {
						for (var i = 0; i < res.data.data.length; i++) {
							let item = res.data.data[i]
							/*res.data[i]*/
							item.semester == 1 ? item.semester = '一' : item.semester = '二'
							let dateTime = Date.now(item.end_time)
							let dateTime2 = Date.now()
							let statesTxt = ''
							if (dateTime2 < dateTime) {
								statesTxt = '已结束'
							}else{
								statesTxt = '进行中'
							}

							this.studentList .push({
								id: item.id,
								termid: item.term_id,
								termname: `${item.start_year}-${item.end_year}学年 第${item.semester}学期`,
								states: statesTxt,
								time: `${item.end_time.substring(0,4)}年${item.end_time.substring(5,7)}月${item.end_time.substring(8,10)}日`
							})
						}
					}

					this.placeholder = 	this.studentList.length != 0 ? false : true
				});

				let params = {
					// term_id: 0,
					class_id: this.path,
					page_index: this.page,
					page_size: this.size
					// user_id: 0,
				}

				this.$ajax({
					url: 'api/quality/quality',
					method: 'get',
					params: params,
					func: {success: success}
				})
			},
			evaluateAdd(){
				this.$router.push({path: '/class/quality/add', query: {id: this.path}})
			},
			goBack(){
				this.$router.back(-1)
			},
			sizeChange(val) {
				// console.log(`每页 ${val} 条`);
				this.page = val
				this.getEvaluate()
			},
			currentChange(val) {
				// console.log(`当前页: ${val}`);
				this.page = 1
				this.size = val;
				this.getEvaluate()
			},
			//删除列表
			removeList(item){

				this.$confirm('是否删除?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning',
					customClass: 'confirm-them'
				}).then(() => {
					let success = ( res => {
						this.$message.success(res.msg);
						this.getEvaluate()
					});
					this.$ajax({
						url: 'api/quality/quality',
						method: 'delete',
						params: {
							class_id: this.path,
							term_id: item.termid
						},
						func: {
							success: success
						}
					})
				}).catch(() => {});
			},
		}
	}
</script>